package com.mall.controller;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.mall.utils.CodeUtil;
import com.mall.utils.R;
import com.wf.captcha.SpecCaptcha;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码获取
 *
 * @author cxx
 */
@Slf4j
@RestController
@Validated
public class CommonController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取验证码，返回结果:key是验证码,image是验证码图片,codeId是表示验证码唯一的id，登录时需要前端返回
     */

    @GetMapping("/captcha")
    public R<Object> captcha(HttpServletRequest request, HttpServletResponse response) {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String verCode = specCaptcha.text().toLowerCase();
        //利用Nanoid生成codeId
        String codeId = "CAPTCHA_" + NanoIdUtils.randomNanoId();
        // 将验证码存入redis并设置过期时间为5分钟
        redisTemplate.opsForValue().set(codeId, verCode, 5, TimeUnit.MINUTES);
        // 将验证码和base64返回给前端
        HashMap<String, Object> map = new HashMap<>(16);
        map.put("key", verCode);
        map.put("image", specCaptcha.toBase64());
        map.put("codeId", codeId);
        return R.success(map);
    }

    /**
     * 获取短信验证码
     *
     * @param userPhone 用户电话号码
     * @return com.mall.utils.R<java.lang.String>
     * @author cxx
     * @date 2023/4/22 22:37
     */
    @GetMapping("/getSms")
    public R<String> getSms(@RequestParam(name = "userPhone") @NotNull @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确") String userPhone) {
        //调用自定义方法生成长度为了6的短信验证码
        String code = CodeUtil.generateValidateCode(6);
        log.error("短信验证码-" + code);
        redisTemplate.opsForValue().set("SMS-" + userPhone, code, 5, TimeUnit.MINUTES);
        //调用阿里云发送短信验证码
        //SmsUtil.sendMessage(userPhone, code);
        return R.success("短信验证码发送成功");
    }

    /**
     * 支付成功后的异步通知
     *
     * @param map
     * @return java.lang.String
     * @author cxx
     * @date 2023/6/1 17:25
     */
    @PostMapping("/notify")
    public String payNotify(@RequestParam Map map) {
        log.error(map.toString());
        log.info("支付通知!!!!!!!!!");
        return "success";
    }

    /**
     * 支付成功后的回调函数，支付成功后自动回调
     *
     * @param
     * @return java.lang.String
     * @author cxx
     * @date 2023/6/1 16:25
     */
    @GetMapping("/afterPay")
    public String afterPay() {
        return "afterPay";
    }
}
